<?php

declare (strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Model;

class NoticeUser extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected ?string $table = 'notice_user';
    public static $statusList = [1 => '未读', 2 => '已读'];
    public function notice()
    {
        return $this->belongsTo(Notice::class, 'notice_id');
    }
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
    public function getStatusTextAttribute()
    {
        return static::$statusList[$this->status] ?? '未读';
    }
    /**
     * 标记为已读
     * @return $this
     */
    public function read()
    {
        if ($this->status != 2) {
            $this->status = 2;
        }
        return $this;
    }
    /**
     * 接口返回
     * @return array
     */
    public function toApi()
    {
        $notice = $this->notice;
        $data = ['id' => $this->id, 'status' => $this->status ?? 1, 'status_text' => $this->status_text, 'title' => $notice->title ?? '', 'content' => $notice->content ?? '', 'create_time' => $this->create_time->toDateTimeString()];
        return $data;
    }
}